// @ts-nocheck
import Util from "./wps_util.ts";
import taskPane from "@/stores/index.ts";
// import SystemDemo from './js/systemdemo.js'
import useSysStore from "../stores/index";

//这个函数在整个wps加载项中是第一个执行的
function OnAddinLoad(ribbonUI) {
  if (typeof wps.ribbonUI != "object") {
    wps.ribbonUI = ribbonUI;
  }

  if (typeof wps.Enum != "object") {
    // 如果没有内置枚举值
    wps.Enum = Util.WPS_Enum;
  }

  //这几个导出函数是给外部业务系统调用的
  //   window.openOfficeFileFromSystemDemo = SystemDemo.openOfficeFileFromSystemDemo;
  //   window.InvokeFromSystemDemo = SystemDemo.InvokeFromSystemDemo;

  // wps.PluginStorage.setItem("EnableFlag", false); //往PluginStorage中设置一个标记，用于控制两个按钮的置灰
  // wps.PluginStorage.setItem("ApiEventFlag", false); //往PluginStorage中设置一个标记，用于控制ApiEvent的按钮label
  return true;
}

// eslint-disable-next-line no-unused-vars

var WebNotifycount = 0;
function OnAction(control) {
  //关闭之前打开的taskpane
  const sysStore = useSysStore();
  let pane_history = sysStore.taskpane;
  if (pane_history) {
    let tsId = wps.PluginStorage.getItem(pane_history);
    try {
      let tskpane = wps.GetTaskPane(tsId);
      tskpane.Visible = false;
      sysStore.taskpane = null;
    } catch (error) {
      console.log(error);
    }
  }

  const eleId = control.Id;
  let tsId = wps.PluginStorage.getItem(eleId);
  if (!tsId) {
    let tskpane = wps.CreateTaskPane(Util.GetUrlPath() + "/" + eleId);
    console.log(tskpane.Width);    
    tskpane.Width=sysStore.taskPane_width
    let id = tskpane.ID;
    wps.PluginStorage.setItem(eleId, id);
    tskpane.Visible = true;
  } else {
    let tskpane = wps.GetTaskPane(tsId);
    tskpane.Visible = !tskpane.Visible;
  }
  sysStore.taskpane = eleId;
  return true;
}

function GetImage(control) {
  const eleId = control.Id;
  // return "images/采购统计.svg";

  console.log(eleId);
  const img_obj = {
    receiptOrder: "订货单",
    receiptBuy: "进货单",
    receiptSale: "销售单",
    receiptOther: "其他单",
    stasticsStock: "库存统计",
    stasticsBuy: "进货统计",
    stasticsSale: "销售统计",
    settingCustomer: "客户设置",
    settingCategory: "类别设置",
    settingGoods: "商品设置",
    settingGoodsUnit: "商品单位设置",
  };  // console.log("images/" + img_obj[eleId] + ".svg");
  return "images/" + img_obj[eleId] + ".svg";
}

function OnGetEnabled(control) {
  const eleId = control.Id;
  switch (eleId) {
    case "btnShowMsg":
      return true;
    case "btnShowDialog": {
      let bFlag = wps.PluginStorage.getItem("EnableFlag");
      return bFlag;
    }
    case "btnShowTaskPane": {
      let bFlag = wps.PluginStorage.getItem("EnableFlag");
      return bFlag;
    }
    default:
      break;
  }
  return true;
}

function OnGetVisible(control) {
  const eleId = control.Id;
  // console.log(eleId);
  return true;
}

function OnGetLabel(control) {
  const eleId = control.Id;
  switch (eleId) {
    case "btnIsEnbable": {
      let bFlag = wps.PluginStorage.getItem("EnableFlag");
      return bFlag ? "按钮Disable" : "按钮Enable";
    }
    case "btnApiEvent": {
      let bFlag = wps.PluginStorage.getItem("ApiEventFlag");
      return bFlag ? "清除新建文件事件" : "注册新建文件事件";
    }
  }
  return "";
}

function OnNewDocumentApiEvent(doc) {
  alert("新建文件事件响应，取文件名: " + doc.Name);
}

//这些函数是给wps客户端调用的
export default {
  OnAddinLoad,
  OnAction,
  GetImage,
  OnGetEnabled,
  OnGetVisible,
  OnGetLabel,
  OnNewDocumentApiEvent,
};
